home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / menu-new.tar / menu-new / menu / dir.c < prev    next >
C/C++ Source or Header  |  1993-04-08  |  16KB  |  722 lines

  1. # include <stdio.h>
  2. # include <sys/types.h>
  3. # include <stdlib.h>
  4.  
  5. # ifndef HPUX
  6. # include <sys/dir.h>
  7. # else
  8. # include <sys/dirent.h>
  9. # endif HPUX
  10.  
  11. # include <sys/stat.h>
  12. # include <signal.h>
  13. # include "menu.h"
  14. # include "dir.h"
  15.  
  16. /*
  17.  *    is_directory(directory,file):  
  18.  *            The first parameter is the name
  19.  *            directory and the second is the
  20.  *            name of the file in the directory.
  21.  *
  22.  *    Purpose:    Determine if a file is a directory file.
  23.  *
  24.  *    Return:        -1 = Failure to stat file, (file may not exist)
  25.  *                1  = Is a directory file.
  26.  *            0  = Is a regular file.
  27.  *
  28.  *    Last Modify:    3-31-91 (TW)
  29.  */
  30.  
  31. int is_directory(file_name)
  32. char *file_name;
  33. {
  34.     struct stat st_buf;
  35.  
  36.     if(stat(file_name,&st_buf) < 0)
  37.       return(-1);
  38.     else
  39.       if(st_buf.st_mode & S_IFDIR)
  40.         return(1);
  41.     return(0);
  42. }
  43.  
  44. /*
  45.  *    void replace_spaces(string):
  46.  *            The Parameter "string" is a file name that may
  47.  *            contain spaces.  Replace all spaces with the
  48.  *            '*' character.
  49.  *
  50.  *    Purpose:    Make it so a file with spaces in the name may
  51.  *            be accessed.
  52.  *
  53.  *    Returns:    None
  54.  *
  55.  *    Last Modify:    5-01-91 (TW)
  56.  *
  57.  */
  58.  
  59. void replace_spaces(string)
  60. char *string;
  61. {
  62.     for(;*string != '\0';string++)
  63.       if(isspace(*string))
  64.         *string = '*';
  65. }
  66.  
  67. /*
  68.  *    int get_dir(dir_entries,name,dirs):
  69.  *            The parameter "dir_entries" an character array of
  70.  *            MAX_ENT entries, with each entry being ENT_LEN in
  71.  *             length. Dirs= let use directories.
  72.  *
  73.  *    Purpose:    Read the current directory file and extract the
  74.  *            file names and sort the list.
  75.  *
  76.  *    Return Values:    -1  = Failed to read the directory file.
  77.  *            ct  = The number of files in the directory, the count
  78.  *            excludes (dot) files and Directory Files.
  79.  *
  80.  *    Last Modify:    3-31-91 (TW)
  81.  *            5-01-91 (TW)
  82.  */
  83.  
  84. int get_dir(dir_entries,name,dirs)
  85. char dir_entries[MAX_ENT][ENT_LEN];
  86. char *name;
  87. int dirs;
  88. {
  89.     DIR *dirp;
  90. # if defined (SUN) || defined (BSD43)
  91.     struct direct *dp;
  92. # else
  93. # ifdef linux
  94.     struct direct *dp;
  95. # else
  96.     struct dirent *dp;
  97. # endif linux
  98. # endif SUN
  99.     char buff[80];
  100.     int i = 0,ct;
  101.     
  102.     if((dirp = opendir(name)) == (DIR *)NULL)    /* Open the directory */
  103.       return(-1);                     /* Return with error  */
  104.     for(dp = readdir(dirp);dp != NULL; dp = readdir(dirp)){
  105. /*       if(dp->d_name[0] != '.' && !is_directory(dp->d_name)){ */
  106.        if(!dirs && is_directory(dp->d_name))
  107.         continue;
  108.          if(strcmp(dp->d_name,".")) {
  109.           if (is_directory(dp->d_name))
  110.         strcat(dp->d_name,"/");
  111.           replace_spaces(dp->d_name);
  112.           memccpy(dir_entries[i],dp->d_name,'\0',ENT_LEN-2);
  113.           dir_entries[i][ENT_LEN-1] = '*';             /* Just in case long name*/
  114.           i++;
  115.           ct = i;
  116.        }
  117.     }
  118.     closedir(dirp);                    /* Close the Directory */
  119.     /* qsort(dir_entries,ct,ENT_LEN,strcmp);    */    /* Sort the List       */
  120.     return(ct);
  121. }
  122.  
  123. /*
  124.  *    int print_dir(dir_entries,start_ent,n_entries,end,dptr)
  125.  *
  126.  *    Parameters:    dir_entries - The list of files.
  127.  *            start_ent   - Start printing with entry #.
  128.  *            n_entries   - How many entries are there.
  129.  *            end         - What # do we stop printing at.
  130.  *            dptr        - Special info.
  131.  *    Purpose:    Print a select group of file names ont the screen.
  132.  *
  133.  *    Returns:    The Number of the last entry printed.
  134.  *
  135.  *    Last Modify:    3-31-91 (TW)
  136.  */
  137.  
  138. int print_dir(dir_entries,start_ent,n_entries,end,dptr)
  139. char dir_entries[MAX_ENT][ENT_LEN];
  140. int start_ent,n_entries,end;
  141. dir_info *dptr;
  142. {
  143.     int i, row = 5, col = 0;
  144.     int this_scr;
  145.  
  146.     print_other();            /* Print Directions on the top of the*/
  147.                     /* Screen.                 */
  148.     dptr->max_row = dptr->max_col = 0;
  149.     this_scr = start_ent + 45;
  150.     if(n_entries > end){
  151.       if(this_scr > n_entries)
  152.          this_scr = n_entries;
  153.     }else{
  154.       this_scr = n_entries;
  155.     }
  156.     /*
  157.      * Print all for this screen.
  158.          */
  159.     for(i=start_ent;i<this_scr;i++){
  160.       if((i != 0) && ((i % 15) == 0) && ((i % 45) != 0)){
  161.          col++;
  162.          row = 5;
  163.       }
  164.       move_csr((col*20)+10,row);
  165.       print_str(dir_entries[i]);
  166.       row++;
  167.       if(dptr->max_row < row-2)
  168.          dptr->max_row = row-2;
  169.       if(dptr->max_col < ((col)+1)*20)
  170.          dptr->max_col = ((col+1)*20);
  171.       
  172.     }
  173.     return(this_scr);
  174. }
  175.  
  176. /*
  177.  *    move_entry(dir_entries,row,col,row_off,start,flag)
  178.  *
  179.  *    Parameters:    dir_entries - The list of file Names.
  180.  *            row         - Which row we are on.
  181.  *            row_off        - Row offset from top of screen.
  182.  *            start        - The array index number of the
  183.  *                      first file name.
  184.  *            flag        - Boolean: True == Highlight
  185.  *                      False == No Highlight
  186.  *
  187.  *    Purpose:    Allow user to move a high-lite bar to select a
  188.  *            file.
  189.  *
  190.  *    Last Modify:    3-31-91 (TW)
  191.  */
  192.  
  193. void move_entry(dir_entries,row,col,row_off,start,flag)
  194. char dir_entries[MAX_ENT][ENT_LEN];
  195. int row,col,row_off,start,flag;
  196. {
  197.     int index;
  198.  
  199.     if(col == 0)
  200.       index = ((col%13))+row+start;
  201.     else
  202.       index = (((col/15)*15))+row+start;
  203.         move_csr(col+10,row+row_off);
  204.     if(!flag)
  205.       print_str(dir_entries[index]);
  206.     else
  207.       hi_lite(dir_entries[index]);
  208. }
  209.  
  210. /*
  211.  *    char *select_item(dir_ent,number,start,end,dp)
  212.  *
  213.  *    parameters:    dir_ent - The list of file Names.
  214.  *            number  - 
  215.  *            start   - The Starting Number for the current screen.
  216.  *            end     - The number of the last one on this screen.
  217.  *            dp      - Special Info.
  218.  *
  219.  *    Purpose:    Loop to get selections. Ultimately returns a file name.
  220.  *
  221.  *    Last Modify:    3-31-91 (TW)
  222.  *            5-01-91 (TW)
  223.  */
  224.  
  225. char *select_item(dir_ent,number,start,end,dp)
  226. char dir_ent[MAX_ENT][ENT_LEN];
  227. int number,start,end;
  228. dir_info *dp;
  229. {
  230.     int row = 0, col = 0, row_off = 5, x;
  231.     int inp;
  232.     char buff[80];
  233.  
  234.     move_csr(col+10,row_off+col);
  235.     hi_lite(dir_ent[0]);
  236.     for(;;){
  237.     switch(inp = get_input()){
  238.        case 'n':            /* Go to next page        */
  239.        case 'N':
  240.            if(number > end){
  241.              start += 45;
  242.              clr_area(2,2,2,76);
  243.              clr_area(3,2,dp->max_row,dp->max_col+5);
  244.              end = print_dir(dir_ent,start,number,end,dp);
  245.              row = col = 0;
  246.              move_entry(dir_ent,row,col,row_off,start,1);
  247.            }
  248.        break;
  249.        case 'p':            /* Go to the previous page    */
  250.           case 'P':
  251.            if(start >= 45){
  252.                   start -= 45;
  253.              clr_area(2,2,2,76);
  254.              clr_area(3,2,dp->max_row,dp->max_col+5);
  255.              end = print_dir(dir_ent,start,number,end-45,dp);
  256.              row = col = 0;
  257.              move_entry(dir_ent,row,col,row_off,start,1);
  258.            }
  259.        break;
  260.        case 'j':            /* Move the hi-lite bar down     */
  261.        case 'J':
  262.        case DOWN:
  263.            if(row != 14){
  264.              move_entry(dir_ent,row,col,row_off,start,0);
  265.              row++;
  266.              move_entry(dir_ent,row,col,row_off,start,1);
  267.            }
  268.        break;
  269.        case 'k':            /* Move The hi-lite bar up    */
  270.        case 'K':
  271.        case UP:
  272.            if(row != 0){
  273.              move_entry(dir_ent,row,col,row_off,start,0);
  274.              row--;
  275.              move_entry(dir_ent,row,col,row_off,start,1);
  276.         }
  277.        break;
  278.        case 'l':            /* Move The hi-lite bar Right    */
  279.        case 'L':
  280.        case RIGHT:
  281.            if(col < 40){
  282.              move_entry(dir_ent,row,col,row_off,start,0);
  283.              col += 20;
  284.              move_entry(dir_ent,row,col,row_off,start,1);
  285.         }
  286.        break;
  287.        case 'h':            /* Move The hi-lite bar Left    */
  288.        case 'H':
  289.        case LEFT:
  290.            if(col != 0){
  291.              move_entry(dir_ent,row,col,row_off,start,0);
  292.              col -= 20;
  293.              move_entry(dir_ent,row,col,row_off,start,1);
  294.         }
  295.        break;
  296.        case '\n':            /* Make a selection        */
  297.        case '\r':
  298.            if(col == 0)
  299.              x = ((col%14))+row+start;
  300.            else
  301.              x = (((col/15)*15))+row+start;
  302.            if(strlen(dir_ent[x]) >= 1){
  303.              clr_area(2,2,2,76);
  304.              clr_area(3,2,dp->max_row,dp->max_col+5);
  305.              clr_area(20,38,1,38);
  306.              return(dir_ent[x]);
  307.             }else{
  308.              clr_area(2,2,2,76);
  309.              clr_area(3,2,dp->max_row,dp->max_col+5);
  310.              clr_area(20,38,1,38);
  311.              return('\0');
  312.            }
  313.        break;
  314.        case 'q':            /* Do Not make a selection    */
  315.             clr_area(2,2,2,76);
  316.             clr_area(3,2,dp->max_row,dp->max_col+5);
  317.             clr_area(20,38,1,38);
  318.             return('\0');
  319.        break;
  320.        default:
  321.        break;
  322.     }
  323.      }
  324. }
  325.  
  326. /*
  327.  *    print_other()
  328.  *    
  329.  *    Parameters:    None
  330.  *
  331.  *     Purpose:    Print Some Instructions.
  332.  *    
  333.  *    Last Modify:    3-31-91 (TW)
  334.  */
  335.  
  336. print_other()
  337. {
  338.  
  339.     move_csr(2,2);
  340.     hi_lite("Select a file?   Use: Arrow Keys to move, Return to select, 'q' to abort"); 
  341.     move_csr(24,3);
  342.     hi_lite("n - next page, p - previous page");
  343.     move_csr(38,20);
  344.     hi_lite("h,j,k,l keys also move selection bar.");
  345. }
  346.  
  347. /*    
  348.  *    int read_printers(printers)
  349.  *    
  350.  *    Parameters:    printers - A list of printers read from file.
  351.  *    
  352.  *    Purpose:    Read printer info. from a file (PRINT_PATH).
  353.  *
  354.  *    Returns:    -1    = Failure.
  355.  *            index = The number of printers defined.
  356.  *
  357.  *    Last Modify:    3-31-91 (TW)
  358.  */
  359.  
  360. int read_printers(printers)
  361. printer printers[MAX_PRINTERS];
  362. {
  363.     FILE *fp;
  364.     int index = 0;
  365.     char buff[256];
  366.     
  367.     if((fp = fopen(PRINT_PATH,"r")) == (FILE *)NULL){
  368.        return(-1);
  369.     }else{
  370.        while(!feof(fp)){
  371.          Fgets(buff,256,fp);
  372.          if(buff[strlen(buff)-1] == '\n')
  373.            buff[strlen(buff)-1] = '\0';
  374.          if(strlen(buff) > 31)
  375.            buff[31] = '\0';
  376.          if(!feof(fp) && (index < MAX_PRINTERS)){
  377.             switch(buff[0]){
  378.            case '?':        /* Text comment user sees */
  379.             memccpy(printers[index].comment,buff+2,'\0',30);
  380.            break;
  381.            case '*':        /* Printing Commmand      */
  382.             memccpy(printers[index].command,buff+2,'\0',30);
  383.             index++;
  384.            break;
  385.            default:
  386.            break;
  387.         }
  388.          }
  389.     }
  390.     fclose(fp);
  391.     }
  392.     return(index);    /* Return Count */
  393. }
  394.  
  395. /*
  396.  *    show_printers(printers,num)
  397.  *
  398.  *    Parameters:    printers - A list of printers read from file.
  399.  *            num      - The Number of printers read.
  400.  *
  401.  *    Purpose:    Print The Printer selections on the screen.
  402.  *
  403.  *    Returns:    None
  404.  *
  405.  *    Last Modify:    3-31-91 (TW);
  406.  */
  407.  
  408. void show_printers(printers,num)
  409. printer printers[MAX_PRINTERS];
  410. int num;
  411. {
  412.     int i, row = 6;
  413.  
  414.     for(i=0;i<num;i++){
  415.       if(!isposodd(i))
  416.         move_csr(5,row);
  417.       else{
  418.         move_csr(45,row);
  419.         row++;
  420.       }
  421.       print_str(printers[i].comment);
  422.     }
  423. }
  424.  
  425. /*
  426.  *    char *choose_print(string)
  427.  *
  428.  *    Parameters:    string - The file to be printed.
  429.  *    
  430.  *    Purpose:    Allow User to choose a printer.
  431.  *
  432.  *    Returns:    A pointer to the printer string.
  433.  *
  434.  *    Last Modify:    3-31-91 (TW)
  435.  */
  436.  
  437. char *choose_print(string)
  438. char *string;
  439. {
  440.     printer printers[MAX_PRINTERS];
  441.     int i = 0, row = 0, col = 1, num,ans,max;
  442.     char buffer[256];
  443.  
  444.     fflush(stdout);
  445.     num = read_printers(printers);
  446.     show_printers(printers,num);
  447.     move_csr(5,2);
  448.     hi_lite(PRT_MENU);
  449.     move_csr(5,4);
  450.     (void) sprintf(buffer,"Filename: %s",string);
  451.     hi_lite(buffer);
  452.     move_csr(5,6);
  453.     hi_lite(printers[i].comment);
  454.     do{
  455.       switch(ans = get_input()){
  456.          case 'j':            /* Move Hi-Lite Bar down    */
  457.          case 'J':
  458.          case DOWN:
  459.           if(col == 1)
  460.             max = in_col_one(num);
  461.           else
  462.             max = in_col_two(num);
  463.           if(row+1 < max){
  464.             if(col == 1)
  465.               move_csr(5,row+6);
  466.               else
  467.               move_csr(45,row+6);
  468.             print_str(printers[i].comment);
  469.             i+=2;
  470.                 row++;
  471.             if(col == 1)
  472.               move_csr(5,row+6);
  473.             else
  474.               move_csr(45,row+6);
  475.             hi_lite(printers[i].comment);
  476.           }
  477.          break;
  478.          case 'k':            /* Move Hi-Lite Bar up      */
  479.          case 'K':
  480.          case UP:
  481.           if(row >= 1){
  482.             if(col == 1)
  483.               move_csr(5,row+6);
  484.             else
  485.               move_csr(45,row+6);
  486.             print_str(printers[i].comment);
  487.             i-=2;
  488.               row--;
  489.             if(col == 1)
  490.               move_csr(5,row+6);
  491.             else
  492.               move_csr(45,row+6);
  493.             hi_lite(printers[i].comment);
  494.           }
  495.          break;
  496.          case 'l':            /* Move Hi-Lite Bar right    */
  497.          case 'L':
  498.          case RIGHT:
  499.              if(col == 1 && ((i + 1) < num)){
  500.             move_csr(5,row+6);
  501.             print_str(printers[i].comment);
  502.             col++;
  503.             i++;
  504.             move_csr(45,row+6);
  505.             hi_lite(printers[i].comment);
  506.               }
  507.          break;
  508.          case 'h':            /* Move Hi-Lite Bar left    */
  509.          case 'H':
  510.          case LEFT:
  511.              if(col > 1){
  512.             move_csr(45,row+6);
  513.             print_str(printers[i].comment);
  514.             col--;
  515.             i--;
  516.             move_csr(5,row+6);
  517.             hi_lite(printers[i].comment);
  518.           }
  519.          break;
  520.          case '\r':
  521.          case '\n':
  522.               return(printers[i].command);
  523.          break;
  524.          case 'q':
  525.          case 'Q':
  526.               return('\0');
  527.          break;
  528.       }
  529.     }while(ans != 'q');
  530.     return('\0');
  531. }
  532.  
  533. /* Not Implemented Completely */
  534.  
  535. # ifdef OPTS
  536.  
  537. /*
  538.  *    print_op(index,FLAG)
  539.  *
  540.  *    Parameters:    index - Selection index (which) parameter doing.
  541.  *            FLAG  - Hi-lite or Not.
  542.  *    
  543.  *    Returns:    None.
  544.  *    
  545.  *    Purpose:    Selecting Print time Options.
  546.  *    
  547.  *    Last Modify:    3-31-91 (TW)
  548.  */
  549.  
  550. void print_op(index,FLAG)
  551. int index;
  552. int FLAG;
  553. {
  554.     char buffer[256];
  555.  
  556.     move_csr(print_opts[index].x,print_opts[index].y);
  557.     if(index == 3){
  558.          if(print_opts[index].option == 1)
  559.               (void) sprintf(buffer,print_opts[index].selection," no");
  560.          else
  561.               (void) sprintf(buffer,print_opts[index].selection,"yes");
  562.          }else{
  563.               (void) sprintf(buffer,print_opts[index].selection,
  564.               print_opts[index].option);
  565.          }
  566.        if(FLAG == 0)
  567.            print_str(buffer);
  568.        else
  569.            hi_lite(buffer);
  570. }
  571.  
  572. /*
  573.  *    get_options(pr_cmd,lpr_cmd)
  574.  *    
  575.  *    Parameters:    pr_cmd  - The PR command and options.
  576.  *            lpr_cmd - The LPR command and options.
  577.  *
  578.  *    Returns:    None.
  579.  *    
  580.  *    Purpose:    Select Special Print Time options.
  581.  *
  582.  *    Last Modify:    3-31-91 (TW)
  583.  */
  584.  
  585. void get_options(pr_cmd,lpr_cmd)
  586. char *pr_cmd, *lpr_cmd;
  587. {
  588.     int index = 0;
  589.     char buffer[256];
  590.     int input;
  591.     
  592.     move_csr(5,18);
  593.     hi_lite("Use Arrow Keys to move, Space or Return to change/select.");
  594.     print_op(0,1);
  595.     print_op(1,0);
  596.     print_op(2,0);
  597.     print_op(3,0);
  598.     print_op(4,0);
  599.     print_op(0,1);
  600.     do{
  601.     input = get_input();
  602.     switch(input){
  603.        case RIGHT:
  604.         if(index < 4){
  605.           print_op(index,0);
  606.               index++;
  607.           print_op(index,1);
  608.          }
  609.        break;
  610.        case LEFT:
  611.         if(index > 0){
  612.           print_op(index,0);
  613.               index--;
  614.           print_op(index,1);
  615.         }
  616.        break;
  617.        case ' ':
  618.        case '\n':
  619.        case '\r':
  620.         if(print_opts[index].option == 1){
  621.           print_op(index,0);
  622.           print_opts[index].option++;
  623.           print_op(index,1);
  624.         }else{
  625.           print_op(index,0);
  626.           print_opts[index].option--;
  627.           print_op(index,1);
  628.         }
  629.         if(index == 0){
  630.           fflush(stdout);
  631.           fprintf(stderr,"Ok, Not printing\n");
  632.           exit(0);
  633.         }else
  634.           if(index == 1){
  635.             if(print_opts[2].option == 2)
  636.                (void) strcat(pr_cmd,"d");
  637.             if(print_opts[3].option == 2)
  638.                  (void) strcat(lpr_cmd," -p");
  639.             if(print_opts[4].option == 2)
  640.              (void) strcat(lpr_cmd," -#2");
  641.             return;
  642.         }
  643.        break;
  644.     }
  645.     }while(2 > 1);
  646. }
  647.  
  648. # endif OPTS
  649.  
  650. /*
  651.  *    char *do_selection(dirs)
  652.  *    
  653.  *    Parameters:    Allow directories
  654.  *    
  655.  *    Purpose:    Control module for selecting a file.
  656.  *
  657.  *    Returns:    The Selected File Name.
  658.  *
  659.  *    Last Modify:    3-31-91 (TW)
  660.  */
  661.  
  662. static dir_info *dp = NULL;
  663. char *do_selection(dirs)
  664. int dirs;
  665. {
  666.     char dir_entries[MAX_ENT][ENT_LEN];
  667.     int start_ent = 0, num = 0, end = 0;
  668.     char *strng, buff[256];
  669.  
  670.     if (!dp)
  671.         dp = (dir_info *) malloc(sizeof(dir_info));
  672.     for (num=0; num<MAX_ENT; num++)
  673.         dir_entries[num][0] = 0;
  674.     while (1) {
  675. # ifndef HPUX
  676.         (void) getwd(buff);
  677. # else
  678.         (void) getcwd(buff,BUF_SIZ);
  679. # endif HPUX
  680.         if((num = get_dir(dir_entries,buff,dirs)) == -1)
  681.           return('\0');
  682.         end = print_dir(dir_entries,start_ent,num,45,dp);
  683.         strng = select_item(dir_entries,num,start_ent,end,dp);
  684.         if (!strchr(strng,'/'))
  685.             return(strng);
  686.         chdir(strng);
  687.     } 
  688. }
  689.  
  690. /*
  691.  *    do_printers()
  692.  *    
  693.  *    Parameters:    None
  694.  *    
  695.  *    Purpose:    Control module for  printing a file
  696.  *
  697.  *    Returns:    None.
  698.  *
  699.  *    Last Modify:    3-31-91 (TW)
  700.  */
  701.  
  702. void do_printers()
  703. {
  704.     char *strng1, *strng2;
  705.     char buff[256];
  706.  
  707.     move_csr(68,STATUS_LINE);
  708.     hi_lite("           ");
  709.     strng1 = do_selection();
  710.     if(strng1 != NULL){
  711.       strng2 = choose_print(strng1);
  712.       if(strng2 != NULL){
  713.          Sprintf(buff,"%s %s",strng2,strng1);
  714.          exec_cshell(buff,CONTINUE);    
  715.       }else{
  716.          clr_area(3,2,19,73);
  717.       }
  718.     }
  719.     move_csr(68,STATUS_LINE);
  720.     hi_lite(CMD_LIN);
  721. }
  722.